﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Windows.Forms;
using RibbonEditor.AppCode;

namespace RibbonEditor.CommandUIDefinitionClasses
{
    public class Label : ICommandUIDefinition, IControlsChild, IXmlBuildable
    {
        public string Command { get; set; }
        public string ForId { get; set; }
        public string Id { get; set; }
        public string Image16by16 { get; set; }
        public int? Image16by16Left { get; set; }
        public int? Image16by16Top { get; set; }
        public string LabelText { get; set; }
        public string QueryCommand { get; set; }
        public int? Sequence { get; set; }
        public string TemplateAlias { get; set; }

        public Label()
        { }

        public Label(XmlNode node)
            : this()
        {
            try
            {
                this.Command = XmlHelper.GetAttribute(node, "Command");
                this.ForId = XmlHelper.GetAttribute(node, "ForId");
                this.Id = XmlHelper.GetAttribute(node, "Id");
                this.Image16by16 = XmlHelper.GetAttribute(node, "Image16by16");
                this.Image16by16Left = XmlHelper.GetNullableIntAttribute(node, "Image16by16Left");
                this.Image16by16Top = XmlHelper.GetNullableIntAttribute(node, "Image16by16Top");
                this.LabelText = XmlHelper.GetAttribute(node, "LabelText");
                this.QueryCommand = XmlHelper.GetAttribute(node, "QueryCommand");
                this.Sequence = XmlHelper.GetNullableIntAttribute(node, "Sequence");
                this.TemplateAlias = XmlHelper.GetAttribute(node, "TemplateAlias");
            }
            catch (Exception error)
            {
                MessageBox.Show("An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        public void AddNode(XmlNode commandUINode)
        {
            try
            {
                XmlNode labelNode = XmlHelper.AddNode(commandUINode, "Label");
                XmlHelper.AddAttribute(labelNode, "Command", this.Command);
                XmlHelper.AddAttribute(labelNode, "ForId", this.ForId);
                XmlHelper.AddAttribute(labelNode, "Id", this.Id);
                XmlHelper.AddAttribute(labelNode, "Image16by16", this.Image16by16);
                XmlHelper.AddAttribute(labelNode, "Image16by16Left", this.Image16by16Left);
                XmlHelper.AddAttribute(labelNode, "Image16by16Top", this.Image16by16Top);
                XmlHelper.AddAttribute(labelNode, "LabelText", this.LabelText);
                XmlHelper.AddAttribute(labelNode, "QueryCommand", this.QueryCommand);
                XmlHelper.AddAttribute(labelNode, "Sequence", this.Sequence);
                XmlHelper.AddAttribute(labelNode, "TemplateAlias", this.TemplateAlias);
            }
            catch (Exception error)
            {
                MessageBox.Show("An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        public void BuildTreeViewElement(TreeNode parentNode)
        {
            try
            {
                TreeNode node = new TreeNode("Label");
                node.Tag = this;
                node.ContextMenuStrip = ContextMenuStripHelper.BuildUICommandDefContextMenuStrip(node, null, true);

                parentNode.Nodes.Add(node);
            }
            catch (Exception error)
            {
                MessageBox.Show("An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
    }
}
